package org.jeecg.modules.ncmeapis.data.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ncme.digital.hszy.common.utils.StatisticUtil;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.ncmeapis.data.entity.DataTemp;
import org.jeecg.modules.ncmeapis.data.mapper.DataTempMapper;
import org.jeecg.modules.ncmeapis.data.service.IDataTempService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@Service
public class DataTempServiceImpl extends ServiceImpl<DataTempMapper, DataTemp> implements IDataTempService {

    @Override
    public void setTempData(String category, String name, String value) {
        DataTemp dataTemp = new DataTemp();
        dataTemp.setCategory(category);
        dataTemp.setName(name);
        dataTemp.setValue(value);
        saveOrUpdate(dataTemp, new LambdaQueryWrapper<DataTemp>()
                .eq(DataTemp::getCategory, category)
                .eq(DataTemp::getName, name));
    }

    @Override
    public Object getTempData(String category, String name, String type) {
        try {
            LambdaQueryWrapper<DataTemp> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DataTemp::getCategory, category);
            wrapper.eq(DataTemp::getName, name);
            List<DataTemp> list = list(wrapper);
            if (list.size() == 0) throw new Exception("数据不存在");
            else if (list.size() > 1) throw new Exception("数据重复：" + category + "-" + name);
            else {
                Object result = null;
                DataTemp dataTemp = list.get(0);
                String value = dataTemp.getValue();
                if (StringUtils.isNotEmpty(type)) dataTemp.setType(type);
                if (dataTemp.getType() == null) throw new Exception("数据未指定类型");
                switch (dataTemp.getType()) {
                    case "JSONObject":
                        result = JSONObject.parseObject(dataTemp.getValue());
                        break;
                    case "JSONArray":
                        result = JSONArray.parseArray(dataTemp.getValue());
                        break;
                    case "String":
                        result = value;
                        break;
                    case "Integer":
                        result = Integer.parseInt(dataTemp.getValue());
                        break;
                    case "Double":
                        result = Double.parseDouble(dataTemp.getValue());
                        break;
                    case "BigDecimal":
                        result = BigDecimal.valueOf(Double.parseDouble(dataTemp.getValue())).setScale(2, RoundingMode.HALF_DOWN);
                        break;
                    case "Boolean":
                        result = Boolean.parseBoolean(dataTemp.getValue());
                        break;
                }
                return result;
            }
        } catch (Exception e) {
            throw new RuntimeException("数据转换失败, " + e.getMessage(), e);
        }
    }

    @Override
    public Object getTempData(String category, String name) {
        return getTempData(category, name, null);
    }

    @Override
    public Map<String, List<DataTemp>> getAllTempData() {
        return StatisticUtil.classifyList(list(), DataTemp::getCategory);
    }

    @Override
    public List<DataTemp> getTempDataBatch(String category) {
        return getAllTempData().get(category);
    }

}
